package com.qianya.system.manage.web.sys.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianya.model.pojo.sys.qo.SysLogQO;
import com.qianya.system.manage.util.AddressUtils.AddressUtils;
import com.qianya.util.BaseCommon;
import com.qianya.system.manage.web.sys.model.SysLog;
import com.qianya.system.manage.web.sys.service.ISysLogService;
import com.qianya.system.manage.web.sys.mapper.SysLogMapper;
import org.springframework.stereotype.Service;
import com.qianya.model.common.CommonResult;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qianya.model.common.ResultCode;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.Date;

/**
* <p>
*  登录日志服务实现类
* </p>
*
* @author lhl
* @since 2020-06-10
*/
@Service
public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> implements ISysLogService {

        /**
         *  查询日志
         * @author lhl
         * @since 2020-06-10
         */
        @Override
        public CommonResult find(SysLogQO sysLog) {
            QueryWrapper<SysLog> queryWrapper=new QueryWrapper<>();

            if (sysLog.getLogType()<3){
                queryWrapper.eq("LOG_TYPE",sysLog.getLogType());
            }else if (sysLog.getLogType()==3){
                queryWrapper.ne("LOG_TYPE",2);
            }else {
                return CommonResult.error(ResultCode.ERROR,"类型错误");
            }
            queryWrapper.eq(StrUtil.isNotBlank(sysLog.getUsername()),"USERNAME",sysLog.getUsername())
                    .gt(sysLog.getBeginTime()!=null,"CREATE_TIME",sysLog.getBeginTime())
                    .lt(sysLog.getEndTime()!=null,"CREATE_TIME",sysLog.getEndTime())
                    .orderByDesc("CREATE_TIME");
            IPage iPage = page(new Page<>(sysLog.getPageNo(),sysLog.getPageSize()),queryWrapper);
            return CommonResult.success(iPage);
        }


        /**
         * 新增日志
         * @author lhl
         * @since 2020-06-10
         */
        @Override
        public CommonResult create(SysLog sysLog,HttpServletRequest request) {
            String token=request.getHeader("token");
            sysLog.setCreateTime(new Date());
            sysLog.setIp(BaseCommon.getIpAddr(request));
            sysLog.setUserAgent(request.getHeader("User-Agent"));
            boolean b = save(sysLog);
            if (b){
                return CommonResult.success();
            }

            return CommonResult.error(ResultCode.ERROR,"操作失败");
        }



    /**
         * 修改日志
         * @author lhl
         * @since 2020-06-10
         */
        @Override
        public CommonResult renewal(SysLog sysLog,HttpServletRequest request) {
            boolean b = updateById(sysLog);
            if (b){
                return CommonResult.success();
            }
            return CommonResult.error(ResultCode.ERROR,"操作失败");
        }


}
